package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.telephony.TelephonyManager;
import android.text.SpannableStringBuilder;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.imay.base.activity.BaseActivity;
import com.imay.live.helper.PreferenceHelper;
import com.imay.live.utils.AppUtil;
import com.imay.base.util.SpannableStringUtils;
import com.imay.live.utils.ToastUtil;
import com.imay.live.R;
import com.imay.live.utils.VersionUtil;

/**
 * create by nick
 * 我的->设置->关于暧魅
 */
public class AboutIMayActivity extends BaseActivity implements View.OnClickListener {


    private LongPressTimer pressTimer;
    public static void start(Context context) {
        Intent starter = new Intent(context, AboutIMayActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_abount_imay;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.setting_abount));
        TextView tv_version = $(R.id.tv_about_version);
        TextView tv_imei_about = $(R.id.tv_imei_about);
        TextView tv_res_version_about = $(R.id.tv_res_version_about);
        TextView tv_url_about = $(R.id.tv_url_about);
        TextView tv_about_email = $(R.id.tv_about_email);
        TextView tv_about_customer_service = $(R.id.tv_about_customer_service);
        TextView tv_channel_about = $(R.id.tv_channel_about);
        tv_version.setText("v"+VersionUtil.getVersionName(this));
        $(R.id.btn_email_copy_about).setOnClickListener(this);
        $(R.id.btn_url_copy_about).setOnClickListener(this);
        setLongPress();
        try {
            String imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
                    .getDeviceId();
            tv_channel_about.setText(AppUtil.getChannel(this));
            tv_imei_about.setText(imei);
            tv_res_version_about.setText(String.format(getString(R.string.core_version), PreferenceHelper.getInstance().getString(PreferenceHelper.COCOSVCORE,"0"),PreferenceHelper.getInstance().getString(PreferenceHelper.COCOSVERSONE,"0")));
            tv_url_about.setText(getTextWithUnderLine(R.string.about_imay_web));
            tv_about_email.setText(getTextWithUnderLine(R.string.about_email_content));
            tv_about_customer_service.setText(getTextWithUnderLine(R.string.about_qq));
        }catch (Exception e){

        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_url_copy_about:
                AppUtil.copy(this,((TextView) $(R.id.tv_url_about)).getText()+"");
                ToastUtil.show(this, R.string.copy_success);
                break;
            case R.id.btn_email_copy_about:
                AppUtil.copy(this,((TextView) $(R.id.tv_about_email)).getText()+"");
                ToastUtil.show(this, R.string.copy_success);
                break;
        }
    }

    private void setLongPress() {
        $(R.id.iv_launcher_about).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_DOWN){
                    if (pressTimer==null){
                        pressTimer= new LongPressTimer(10000,1000);
                    }
                    pressTimer.start();
                } else if (event.getAction()==MotionEvent.ACTION_UP) {
                    pressTimer.cancel();
                }
                return true;
            }
        });

    }

    private class LongPressTimer extends CountDownTimer {


        public LongPressTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            ToastUtil.show(AboutIMayActivity.this,millisUntilFinished+"");

        }

        @Override
        public void onFinish() {
           $(R.id.ll_system_info_about).setVisibility(View.VISIBLE);
        }
    }

    private SpannableStringBuilder getTextWithUnderLine(int stringId){

        return SpannableStringUtils.getBuilder(getString(stringId)).setUnderline().create();

    }

}
